package com.nf.health.app.adapter;


import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nf.health.app.R;
import com.nf.health.app.models.CheckItem;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 健康自测
 *
 * @author jiax
 */
public class HealthCheckAdapter2 extends BaseAdapter{
    private Context context ;
    private Collection<CheckItem> mDatas;
    private LayoutInflater inflater;
    public HealthCheckAdapter2(Context context , Collection<CheckItem> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mDatas == null ? 0 :mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        if (mDatas instanceof List) {
            return ((List<CheckItem>) mDatas).get(position);
        } else if (mDatas instanceof Set) {
            return new ArrayList<CheckItem>(mDatas).get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View viewRoot = convertView;
        final ViewHolder holder;
        if(viewRoot == null){
            holder = new ViewHolder();
            viewRoot = inflater.inflate(R.layout.item_check,null);
            holder.iv_img = (ImageView) viewRoot.findViewById(R.id.iv_img);
            holder.iv_health_card = (ImageView) viewRoot.findViewById(R.id.iv_health_card);
            holder.tv_testing_title = (TextView) viewRoot.findViewById(R.id.tv_testing_title);
            holder.tv_testing_value = (TextView) viewRoot.findViewById(R.id.tv_testing_value);
            holder.rl_item_bk = (RelativeLayout) viewRoot.findViewById(R.id.rl_item_bk);
            viewRoot.setTag(holder);
        }else{
            holder = (ViewHolder) viewRoot.getTag();
        }

        CheckItem item = ((List<CheckItem>) mDatas).get(position);;
        holder.tv_testing_title.setText( ""+item.getTitle());
        holder.tv_testing_value.setText(""+ item.getDescs());
        if(item.getTitle().equals("中医体检")){
            holder.iv_health_card.setVisibility(View.VISIBLE);
        }else if(item.getTitle().equals("风险评估")){
            holder.iv_health_card.setVisibility(View.VISIBLE);
        }else{
            holder.iv_health_card.setVisibility(View.GONE);
        }


        if(item.getAutoid().equals("1")){
            holder.rl_item_bk.setBackgroundResource(R.drawable.bk_testing_1);
            holder.iv_health_card.setVisibility(View.INVISIBLE);
        }else if(item.getAutoid().equals("2")){
            holder.rl_item_bk.setBackgroundResource(R.drawable.bk_testing_2);
            holder.iv_health_card.setVisibility(View.INVISIBLE);
        }else if(item.getAutoid().equals("3")){
            holder.rl_item_bk.setBackgroundResource(R.drawable.bk_testing_3);
            holder.iv_health_card.setVisibility(View.INVISIBLE);
        }else if(item.getAutoid().equals("4")){
            holder.rl_item_bk.setBackgroundResource(R.drawable.bk_testing_4);
            holder.iv_health_card.setVisibility(View.INVISIBLE);
        }else if(item.getAutoid().equals("5")){
            holder.rl_item_bk.setBackgroundResource(R.drawable.bk_testing_5);
            holder.iv_health_card.setVisibility(View.INVISIBLE);
        }else  {
            holder.rl_item_bk.setBackgroundResource(R.drawable.bk_testing_3);
            holder.iv_health_card.setVisibility(View.INVISIBLE);
        }

        if ("2".equals(item.getType())) {
            if ("subhealth".equals(item.getSearchname())) {
            }
        }
        if ("2".equals(item.getType())) {
            if ("lifequality".equals(item.getSearchname())) {
            }
        }
        if ("2".equals(item.getType())) {
            if ("chinesemedicine".equals(item.getSearchname())) {
            }
        }
        if ("2".equals(item.getType())) {
            if ("healthscore".equals(item.getSearchname())) {
            }
        }
        if ("2".equals(item.getType())) {
            if ("shenghuofangshi".equals(item.getSearchname())) {

            }
        }
        if ("-1".equals(item.getType())) {
            holder.rl_item_bk.setBackgroundResource(R.drawable.bk_testing_6);
            holder.iv_health_card.setVisibility(View.VISIBLE);
//            holder.iv_img.setImageResource(0);
            holder.iv_img.setBackgroundResource(R.drawable.img_testing_zhongyi); //中医体侧图片
        } else if ("-2".equals(item.getType())) {
            holder.rl_item_bk.setBackgroundResource(R.drawable.bk_testing_7);
            holder.iv_health_card.setVisibility(View.VISIBLE);
//            holder.iv_img.setImageResource(0);
            holder.iv_img.setBackgroundResource(R.drawable.img_testing_fengxianpinggu);//风险评估图片
        }else{
//            setImageBitmap2(holder.iv_img, item.getImg(),R.drawable.default_image);
            holder.iv_img.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(context)
                    .load(item.getImg())
                    .placeholder(R.drawable.default_image)
                    .into(holder.iv_img);

        }
        return viewRoot;
    }
    private class ViewHolder{
        ImageView iv_img,iv_health_card;
        TextView tv_testing_title,tv_testing_value;
        RelativeLayout rl_item_bk;
    }
    /**
     * 为ImageView设置图片
     *
     *
     *  解决ImageLoader的闪烁问题
     * @return
     */
    public void setImageBitmap2(final ImageView imageView,String url, final int defaultImageResouce) {
            ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                imageView.setImageResource(defaultImageResouce);
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                imageView.setImageResource(defaultImageResouce);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                imageView.setImageBitmap(loadedImage);
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
        });
    }
}
